package com.zzyl.serve.job;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.zzyl.common.thread.UserThreadLocal;
import com.zzyl.common.utils.SecurityUtils;
import com.zzyl.nursing.domain.CheckIn;
import com.zzyl.nursing.domain.Elder;
import com.zzyl.nursing.domain.NursingElder;
import com.zzyl.nursing.mapper.CheckInMapper;
import com.zzyl.nursing.mapper.ElderMapper;
import com.zzyl.nursing.mapper.NursingElderMapper;
import com.zzyl.serve.domain.NursingLevel;
import com.zzyl.serve.domain.NursingProject;
import com.zzyl.serve.domain.NursingProjectPlan;
import com.zzyl.serve.domain.NursingTask;
import com.zzyl.serve.mapper.NursingLevelMapper;
import com.zzyl.serve.mapper.NursingProjectMapper;
import com.zzyl.serve.mapper.NursingProjectPlanMapper;
import com.zzyl.serve.service.INursingTaskService;
import com.zzyl.serve.service.impl.NursingTaskServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Component
@Slf4j
public class NursingTaskJob {

    @Autowired
    private INursingTaskService nursingTaskService;
    @Autowired
    private CheckInMapper checkInMapper;

    @Autowired
    private ElderMapper elderMapper;

    @Autowired
    private NursingElderMapper nursingElderMapper;

    @Autowired
    private NursingLevelMapper nursingLevelMapper;

    @Autowired
    private NursingProjectPlanMapper nursingProjectPlanMapper;

    @Autowired
    private NursingProjectMapper nursingProjectMapper;

    public void addNursingTask() {
        //入住老人列表
        List<CheckIn> checkInList = checkInMapper.selectList(Wrappers.<CheckIn>lambdaQuery().eq(CheckIn::getStatus, 0));
        for (CheckIn checkIn : checkInList) {
            //获取入住老人
            Elder elder = elderMapper.selectById(checkIn.getElderId());
            if (elder == null){
                continue;
            }
            //老人id
            Long elderId = elder.getId();
            //老人名称
            String elderName = elder.getName();
            //老人入住的床位编号
            String bedNumber = elder.getBedNumber();
            //查询护理员id
            List<NursingElder> nursingElders = nursingElderMapper.selectList(Wrappers.<NursingElder>lambdaQuery().eq(NursingElder::getElderId, elderId));
            ArrayList<Long> nursingIds = new ArrayList<>();
            nursingElders.forEach(nursingElder -> {
                if (nursingElder != null){
                    //获取护理员id
                    nursingIds.add(nursingElder.getNursingId());
                }
            });
            //根据入住表中的 护理等级名称 查询护理计划id，
            String nursingLevelName = checkIn.getNursingLevelName();
            if (nursingLevelName == null || nursingLevelName.isEmpty()) {
                log.warn("入住信息中护理等级名称为空，跳过处理。老人ID: {}", checkIn.getElderId());
                continue;
            }
            NursingLevel nursingLevel = nursingLevelMapper.selectOne(Wrappers.<NursingLevel>lambdaQuery().eq(NursingLevel::getName, nursingLevelName));
            Long planId = nursingLevel.getPlanId();
            //根据护理计划id查询护理项目id，项目名称
            List<NursingProjectPlan> nursingProjectPlans = nursingProjectPlanMapper.selectList(Wrappers.<NursingProjectPlan>lambdaQuery().eq(NursingProjectPlan::getPlanId, planId));
            ArrayList<String> nursingProjectNames = new ArrayList<>();
            ArrayList<Long> projectIds = new ArrayList<>();
            nursingProjectPlans.forEach(nursingProjectPlan -> {
                NursingProject nursingProject = nursingProjectMapper.selectById(nursingProjectPlan.getProjectId());
                if (nursingProject != null){
                    nursingProjectNames.add(nursingProject.getName());
                    projectIds.add(nursingProject.getId());
                }
            });
            System.out.println("projectIds = " + projectIds);

            //查询护理项目id，项目名称

            NursingTask nursingTask = NursingTask.builder()
                    .nursingId(nursingIds.toString().substring(1, nursingIds.toString().length()-1))
                    .projectId(Integer.valueOf(projectIds.get(0)+""))
                    .projectName(nursingProjectNames.get(0).toString())
                    .elderId(elderId)
                    .elderName(elderName)
                    .bedNumber(bedNumber)
                    .estimatedServerTime(LocalDateTime.now().plusHours(8))
                    //.createBy(String.valueOf(SecurityUtils.getLoginUser().getUserId()))
                    .status(1l)
                    .build();

            nursingTaskService.insertNursingTask(nursingTask);
        }

    }
}
